<!--<link rel="stylesheet" href="/admin/assets/module/formSelects/formSelects-v4.css"/>-->
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form" action="">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">订单编号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="code" class="layui-input" type="text"
                                                   placeholder="请输入订单编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">订单来源：</label>
                                        <div class="layui-input-inline mr0">
                                            <select name="orderSource" lay-filter="orderSource">
                                                <option value="">订单来源</option>
                                                <option value="staff">自主录入/员工录入</option>
                                                <option value="platform">云端制造/平台指派</option>
                                                <option value="agent">渠道录入/代理商录入</option>
                                                <option value="customer">云端智采/客户直接委派</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">订单类型：</label>
                                        <div class="layui-input-inline mr0">
                                            <select name="orderType" lay-filter="orderType">
                                                <option value="">订单类型</option>
                                                <option value="proff">打样订单</option>
                                                <option value="trial">试产订单</option>
                                                <option value="mass">量产订单</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button type="button" class="layui-btn icon-btn color-green"
                                                lay-filter="formalOrderSearch" lay-submit>查询
                                        </button>
                                        <button type="reset" class="layui-btn icon-btn color-white" lay-filter="resets"
                                                lay-submit>重置
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--<div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button type="button" id="formalOrderBtnAdd" class="layui-btn icon-btn color-reseda">
                                    添加正式订单信息
                                </button>
                            </div>
                            <div class="lay-btn-rt">
                            </div>
                        </div>
                    </div>-->
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="formalOrderTable" lay-filter="formalOrderTable"></table>
        </div>
    </div>
</div>

<style>
    .open-details-box::-webkit-scrollbar {
        width: 0 !important
    }

    .readonlyNoBorder .layui-input-inline input[readonly]:not(.layui-unselect),
    .readonlyNoBorder .layui-input-inline input[readonly]:not(.layui-unselect):hover,
    .readonlyNoBorder .layui-input-inline input[readonly]:not(.layui-unselect):focus {
        border-color: transparent !important;
        outline-color: transparent;
        color: #999;
    }

    .readonlyNoBorder .layui-form-label {
        width: 100px;
    }

    .readonlyNoBorder > .layui-inline {
        box-sizing: border-box;
        margin-right: 0;
    }

    .examineProductionPlanForm-title {
        width: 100%;

    }

    .examineProductionPlanForm-title .title {
        display: inline-block;
        font-size: 16px;
        font-weight: bold;
        color: #333333;
        line-height: 19px;
        position: relative;
        padding: 6px 0 19px 0;
    }

    .examineProductionPlanForm-title .title:after {
        content: '';
        display: block;
        position: absolute;
        left: 0;
        bottom: 0;
        width: calc(100% + 6px);
        height: 3px;
        background: #0A8B0A;
    }


    div[lay-id="createProductionPlanTable"] .layui-table-cell,
    div[lay-id='addProductionPlanTable'] .layui-table-cell,
    form[lay-filter="examineProductionPlanForm"] .layui-table-cell,
    form[lay-filter="orderDetailsTable"] .layui-table-cell {
        height: auto;
        word-break: break-word;
    }

    form[lay-filter="addProductionPlanForm"] textarea,
    form[lay-filter="examineProductionPlanForm"] textarea {
        width: 90%;
        resize: none;
        padding: 5px 10px;
        height: 100px;
        box-sizing: border-box;
        resize: none;
    }

    form[lay-filter="examineProductionPlanForm"] .layui-table-cell > img {
        height: 100%;
    }
</style>

<!-- 表格操作列 -->
<script type="text/html" id="formalOrderTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="creationPlan">创建生产计划</a>
</script>


<!--创建生产计划弹窗-->
<!--CreateProductionPlan-->
<script type="text/html" id="createProductionPlanForm">
    <form lay-filter="createProductionPlanForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item layui-row readonlyNoBorder">
                    <input name="id" type="hidden"/>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">客户公司名称</label>
                        <div class="layui-input-inline">
                            <input name="customerName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">客户联系人</label>
                        <div class="layui-input-inline">
                            <input name="customerContactsName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">联系人电话</label>
                        <div class="layui-input-inline">
                            <input name="customerContactsPhone" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">交货时间</label>
                        <div class="layui-input-inline">
                            <input name="deliveryTime" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">所属渠道</label>
                        <div class="layui-input-inline">
                            <input name="channelName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">订单录入人</label>
                        <div class="layui-input-inline">
                            <input name="creatorName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <input name="checkerName" type="hidden"/>
                        <label class="layui-form-label">询价审核人</label>
                        <div class="layui-input-inline">
                            <input name="checkerName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">报价截止时间</label>
                        <div class="layui-input-inline">
                            <input name="offerEndTime" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <table class="layui-table" id="createProductionPlanTable"
                           lay-filter="createProductionPlanTable"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>

<!--创建生产计划弹窗表格弹窗-->
<!--createProductionPlanTableBar-->
<script type="text/html" id="createProductionPlanTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="proofing">打样计划</a>
    <a class="layui-btn layui-btn-xs" lay-event="trial">试产计划</a>
    <a class="layui-btn layui-btn-xs" lay-event="batch">量产计划</a>
</script>

<!--正式订单排产-->
<!--OrderScheduling-->
<script type="text/html" id="orderSchedulingForm">
    <form lay-filter="orderSchedulingForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <button class="layui-btn icon-btn color-reseda" type="button" id="createOrderScheduling">创建生产计划
                    </button>
                </div>
                <div class="layui-form-item">
                    <table class="layui-table" id="orderSchedulingTable"
                           lay-filter="orderSchedulingTable"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>

<!--正式订单排产table操作-->
<script type="text/html" id="orderSchedulingTableBar">

    {{# if(d.status=="cancellation"){  }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
    {{# }else{ }}
    {{# if(d.whetherEdit=="0"&& d.status =="toconfirmed"){  }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    {{# } }}

    <!-- 订单状态-->
    <!--checkpending(0,待审核);
    checked(1,已审核);
    rowproduce(2,待排产);
    alreadyrowproduce(3,已排产);
    tobeproduced(4,待生产);
    inproduction(5,生产中);
    accomplish(6,完成);-->
    <!--  当订单状态等于以上状态的时候不进行处理  等于其他状态的时候就显示确认提交按钮-->
    {{# if(d.whetherEdit=="0"&& d.status=="affirm"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="checkpending"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="checked"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="rowproduce"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="alreadyrowproduce"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="tobeproduce"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="inproduction"){  }}
    {{# }else if(d.whetherEdit=="0"&& d.status=="accomplish"){  }}
    {{# }else {  }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="confirmSubmission">确认提交</a>
    {{# } }}


    {{# if(d.status=="affirm"){  }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="examine">审核</a>
    {{# } }}

    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="toVoid">作废</a>
    <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>

    {{# if(d.whetherProduction=="0" && d.checkStatus=='approve'){  }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="plan">计划排产</a>
    {{# } }}

    {{# if(d.whetherProduction=="0" && d.status=='alreadyrowproduce'){  }}
    <a class="layui-btn layui-btn-xs" lay-event="checkPlan">排产明细</a>
    {{# } }}

    {{# } }}


</script>

<!--正式订单排产详情-->
<!--examineProductionPlanForm-->
<script type="text/html" id="examineProductionPlanForm">
    <form lay-filter="examineProductionPlanForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item">
                    <label class="layui-form-label">订单来源</label>
                    <div class="layui-input-inline">
                        <input name="planSourceDesc" class="layui-input" type="text" readonly/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="examineProductionPlanForm-title">
                        <h3 class="title">订单信息</h3>
                    </div>
                    <div class="line"></div>
                    <div class="layui-form-item">
                        <!-- 订单表格 -->
                        <table class="layui-table" id="examineProductionOrderTable"
                               lay-filter="examineProductionOrderTable"></table>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="examineProductionPlanForm-title">
                        <h3 class="title">计划信息</h3>
                    </div>
                    <div class="line"></div>
                    <div class="layui-form-item">
                        <!-- 计划表格 -->
                        <table class="layui-table" id="examineProductionPlanTable"
                               lay-filter="examineProductionPlanTable"></table>
                    </div>
                </div>
                <div class="layui-form-item" id="examineBox">
                    <div class="examineProductionPlanForm-title">
                        <h3 class="title">审核信息</h3>
                    </div>
                    <div class="line"></div>
                    <br>
                    <div class="layui-form-item">
                        <div class="layui-col-xs6">
                            <label class="layui-form-label">审核人：</label>
                            <div class="layui-input-inline">
                                <input name="auditPerson" class="layui-input" type="text" readonly/>
                            </div>
                        </div>
                        <div class="layui-col-xs6">
                            <label class="layui-form-label">审核结果：</label>
                            <div class="layui-input-inline">
                                <select name="checkStatus" lay-filter="">
                                    <option value="approve">审核通过</option>
                                    <option value="reject">驳回</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">审核备注：</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入审核备注"></textarea>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="examineProductionPlanForm-title">
                        <h3 class="title">历史审核记录</h3>
                    </div>
                    <div class="line"></div>
                    <div class="layui-form-item">
                        <!-- 计划表格 -->
                        <table class="layui-table" id="auditRecordsTable" lay-filter="auditRecordsTable"></table>
                    </div>
                </div>

            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="examineProductionPlanFormSubmit" lay-submit>提交</button>
            </div>
        </div>
    </form>
</script>

<!--创建生产计划弹窗-->
<!--AddProductionPlan-->
<script type="text/html" id="addProductionPlanForm">
    <form lay-filter="addProductionPlanForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item layui-row readonlyNoBorder">
                    <input name="id" type="hidden"/>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">计划类型</label>
                        <div class="layui-input-inline">
                            <input name="orderTypeDesc" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">交货时间</label>
                        <div class="layui-input-inline">
                            <input name="deliveryTime" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">订单录入人</label>
                        <div class="layui-input-inline">
                            <input name="creatorName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">联系人电话</label>
                        <div class="layui-input-inline">
                            <input name="customerContactsPhone" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <input name="checkerName" type="hidden"/>
                        <label class="layui-form-label">询价审核人</label>
                        <div class="layui-input-inline">
                            <input name="checkerName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">计划性质：</label>
                        <div class="layui-input-inline">
                            <select name="planNature" lay-filter="planNature">
                                <option value="entirety">整体生产计划</option>
                                <option value="process">工序生产计划</option>
                            </select>
                        </div>
                    </div>


                    <div class="layui-inline layui-col-md3" id="processIdBox" style="display: none">
                        <label class="layui-form-label">选择工序</label>
                        <div class="layui-input-inline">
                            <select name="processId" lay-search=""></select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <table class="layui-table" id="addProductionPlanTable"
                           lay-filter="addProductionPlanTable"></table>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注说明</label>
                    <div class="layui-input-block">
                        <textarea name="remark"></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="addProductionPlanFormSubmit" lay-submit="" type="button">提交</button>
            </div>
        </div>
    </form>
</script>

<!--详情-->
<!--Order Details-->
<script type="text/html" id="orderDetailsForm">
    <form lay-filter="orderDetailsForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-form-item layui-row readonlyNoBorder" style="margin: 0">
                    <input name="id" type="hidden"/>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">客户公司名称</label>
                        <div class="layui-input-inline">
                            <input name="customerName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">客户联系人</label>
                        <div class="layui-input-inline">
                            <input name="customerContactsName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">联系人电话</label>
                        <div class="layui-input-inline">
                            <input name="customerContactsPhone" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">交货时间</label>
                        <div class="layui-input-inline">
                            <input name="deliveryTime" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">所属渠道</label>
                        <div class="layui-input-inline">
                            <input name="channelName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">订单类型</label>
                        <div class="layui-input-inline">
                            <input name="orderTypeDesc" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">订单录入人</label>
                        <div class="layui-input-inline">
                            <input name="creatorName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <input name="checkerName" type="hidden"/>
                        <label class="layui-form-label">询价审核人</label>
                        <div class="layui-input-inline">
                            <input name="checkerName" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                    <div class="layui-inline layui-col-md3">
                        <label class="layui-form-label">报价截止时间</label>
                        <div class="layui-input-inline">
                            <input name="offerEndTime" class="layui-input" type="text" readonly/>
                        </div>
                    </div>
                </div>
                <table class="layui-table" id="orderDetailsTable"
                       lay-filter="orderDetailsTable"></table>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate', 'layedit'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var layedit = layui.layedit;
        var laydate = layui.laydate;
        let createProduction = '';

        // 渲染表格
        var insTb = table.render({
            elem: '#formalOrderTable',

            url: config.base + 'enterprise/sell/admin/massOrder/list',
          //  url: config.base + 'production/productionPlan/admin/formalOrder/list?whetherList=1',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 60}
                , {field: 'code', align: 'center', title: '订单编号'}
                , {field: 'orderSourceDesc', align: 'center', title: '订单来源', width: 180}
                , {field: 'customerName', align: 'center', title: '客户名称'}
                , {field: 'orderTypeDesc', align: 'center', title: '订单类型'}
                , {field: 'proQuantity', align: 'center', title: '物料数量'}
                , {field: 'statusDesc', align: 'center', title: '订单状态'}
                , {field: 'creatorName', align: 'center', title: '添加人'}
                , {field: 'createTime', align: 'center', title: '添加时间', width: 160}
                , {align: 'center', toolbar: '#formalOrderTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
            }
        });
        form.render('select');
        // 工具条点击事件
        table.on('tool(formalOrderTable)', function (obj) {
            let data = obj.data;
            if (obj.event === 'detail') { //详情
                showOrderDetailsForm(data)
            } else if (obj.event === 'creationPlan') { // 创建计划
                createProduction = data;
                showCreateProduction(data, 'creationPlan')
            }
        });
        //监听排序
        table.on('sort(formalOrderTable)', function (obj) {
            table.reload('formalOrderTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });
        // 搜索
        form.on('submit(formalOrderSearch)', function (data) {
            insTb.reload({
                where: data.field, page: {curr: 1}
            });
            return false;
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
            return false
        });

        // 导出excel
        $('#inventoryInoutBtnExport').click(function () {
            var checkRows = table.checkStatus('formalOrderTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

        // 询价订单转正式订单
        function showCreateProduction(_data) {
            admin.open({
                type: 1,
                area: ['1500px', '700px'],
                title: _data ? '询价订单转正式订单' : '添加询价订单',
                content: $('#createProductionPlanForm').html(),
                success: function () {
                    form.val('createProductionPlanForm', _data);
                    let createProductionPlanTb = table.render({
                        elem: '#createProductionPlanTable',
                        defaultToolbar: false,
                        data: _data.orderProList,
                        page: false,
                        limit: Number.MAX_VALUE,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            , {field: 'proTypeName', title: '物料类型'}
                            , {field: 'customerSkuName', title: '客户物料名称'}
                            , {field: 'customerSpecCode', title: '客户规格码'}
                            , {
                                field: 'orderProEnclosureList', title: '附件', templet: d => {
                                    let orderProEnclosureListHtml = '';
                                    d.orderProEnclosureList.forEach((item, index) => {
                                        orderProEnclosureListHtml += '<a href="' + item.fileUrl + '" download="12" target="_blank" class="layui-btn layui-btn-xs layui-btn-warm">查看附件</a></br />'
                                    });
                                    return orderProEnclosureListHtml;
                                }
                            }
                            , {field: 'proUnitName', title: '单位'}
                            , {field: 'quantity', title: '需求数量'}
                            , {field: 'statusDesc', title: '状态'}
                            , {align: 'center', toolbar: '#createProductionPlanTableBar', title: '操作', width: 240}
                        ]],
                        done: function (res, curr, count) {

                        }
                    });

                    table.on('tool(createProductionPlanTable)', function (obj) {
                        let data = obj.data;
                        if (obj.event === 'proofing') { //打样计划
                            showOrderScheduling(createProduction, '正式订单打样排产', 'proff', data);
                        } else if (obj.event === 'trial') { // 试产计划
                            showOrderScheduling(createProduction, '正式订单试产排产', 'trial', data);
                        } else if (obj.event === 'batch') {  // 量产计划
                            showOrderScheduling(createProduction, '正式订单量产排产', 'mass', data);
                        }
                    });
                }
            });
        }

        // 正式订单(打样、试产、量产)排产
        function showOrderScheduling(_data, _title, _type, _subData) {
            admin.open({
                type: 1,
                area: ['1550px', '750px'],
                title: _title,
                content: $('#orderSchedulingForm').html(),
                success: function () {
                    form.val('createProductionPlanForm', _data);
                    let orderSchedulingTb = table.render({
                        elem: '#orderSchedulingTable',
                        url: config.base + 'production/productionPlan/admin/productionPlan/list?status=affirm&orderType=' + _type + '&planSource=order',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            , {field: 'code', align: 'center', title: '计划编号', width: 200}
                            , {field: 'orderTypeDesc', align: 'center', title: '计划订单类型'}
                            , {field: 'customerSkuCode', align: 'center', title: '生产物料'}
                            , {field: 'customerSkuSpec', align: 'center', title: '物料规格'}
                            , {field: 'productTypeName', align: 'center', title: '所属物料系列'}
                            , {field: 'productionQuantity', align: 'center', title: '生产数量'}
                            , {field: 'shipmentTime', align: 'center', title: '交货时间', width: 160}
                            , {field: 'statusDesc', align: 'center', title: '状态'}
                            , {field: 'createTime', align: 'center', title: '创建时间', width: 160}
                            , {align: 'center', toolbar: '#orderSchedulingTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {

                        }
                    });

                    // 创建生产计划
                    $('#createOrderScheduling').click(function () {
                        showAddProductionPlan(_data, _subData)
                    });
                    table.on('tool(orderSchedulingTable)', function (obj) {
                        let data = obj.data;
                        if (obj.event === 'details') { //详情
                            examineProductionPlanModel(data, 'details')
                        } else if (obj.event === 'examine') { // 审核
                            examineProductionPlanModel(data, 'examine')
                        } else if (obj.event === 'toVoid') {  // 作废
                            toVoidToolCabinet(data)
                        }
                    });
                }
            });
        }

        // 正式订单(打样、试产、量产)排产详情
        function examineProductionPlanModel(data, _type) {
            admin.open({
                type: 1,
                area: ['1600px', '800px'],
                title: _type == 'examine' ? '生产计划审核' : '生产计划详情',
                content: $('#examineProductionPlanForm').html(),
                success: function () {
                    if (_type == 'details') {
                        $('#examineBox').hide();
                        $('form[lay-filter="examineProductionPlanForm"] button[lay-filter="examineProductionPlanFormSubmit"]').hide()
                    }
                    let examineProductionPlanFormData = [];
                    admin.reqSync('production/productionPlan/admin/productionPlan/getProductionPlanInfoById', {id: data.id}, res => {
                        if (res.code === 200) {
                            examineProductionPlanFormData = res.data;
                        }
                    }, 'get');
                    examineProductionPlanFormData.auditPerson = config.getUser().username;
                    form.val('examineProductionPlanForm', examineProductionPlanFormData);
                    form.render("select");
                    let examineProductionOrderTb = table.render({
                        elem: '#examineProductionOrderTable',
                        data: examineProductionPlanFormData.planData,
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'orderTypeDesc', align: 'center', title: '订单类型'}
                            , {field: 'orderCode', align: 'center', title: '订单编号', width: 150}
                            , {field: 'signOrNotDesc', align: 'center', title: '是否签约'}
                            , {field: 'contractName', align: 'center', title: '所属合同', width: 140}
                            , {field: 'productTypeName', align: 'center', title: '物料类型'}
                            , {field: 'customerSkuCode', align: 'center', title: '规格码'}
                            , {field: 'customerSkuName', align: 'center', title: '物料名称'}
                            , {field: 'customerSkuSpec', align: 'center', title: '物料规格码'}
                            , {
                                field: 'accessoryList', align: 'center', title: '图纸/附件',
                                templet: d => {
                                    let accessoryListHtml = '';
                                    d.accessoryList.forEach((item, index) => {
                                        accessoryListHtml += '<a href="' + item + '" target="_blank" class="layui-btn layui-btn-xs layui-btn-warm">查看附件</a></br />'
                                    });
                                    return accessoryListHtml;
                                }
                            }
                            , {field: 'deliveryTime', align: 'center', title: '交货时间', width: 160}
                            // , {field: 'orderRemark', align:'center', title: '备注'}
                            , {field: 'orderCreatorName', align: 'center', title: '录入人', width: 140}
                            , {field: 'orderCreateTime ', align: 'center', title: '提交时间', width: 160}
                        ]],
                    });

                    let examineProductionPlanTb = table.render({
                        elem: '#examineProductionPlanTable',
                        data: examineProductionPlanFormData.planData,
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'code', align: 'center', title: '计划编号', width: 160}
                            , {field: 'categoryName', align: 'center', title: '计划分类'}
                            , {field: 'typeName', align: 'center', title: '计划类型'}
                            , {field: 'planNatureDesc', align: 'center', title: '计划性质'}
                            , {field: 'demandQuantity', align: 'center', title: '需求量', width: 100}
                            , {field: 'surplusQuantity', align: 'center', title: '剩余计划量'}
                            , {field: 'productionQuantity', align: 'center', title: '本计划生产量'}
                            , {field: 'packName', align: 'center', title: '包装方案'}
                            , {field: 'outputTime', align: 'center', title: '计划产出时间', width: 160}
                            // , {field: 'remark', align:'center', title: '计划创建备注'}
                            , {
                                field: 'creatorName', align: 'center', title: '创建人', width: 200,
                                templet: d => d.creatorName + '<br />电话：' + d.phone
                            }
                            , {field: 'createTime', align: 'center', title: '计划时间', width: 160}
                        ]],
                    });
                    let auditRecordsTb = table.render({
                        elem: '#auditRecordsTable',
                        data: examineProductionPlanFormData.auditData,
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', align: 'center', title: '序号', width: 80}
                            , {field: 'creatorName', align: 'center', title: '审核人'}
                            , {field: 'checkStatusDesc', align: 'center', title: '审核结果'}
                            , {field: 'remark', align: 'center', title: '审核备注'}
                            , {field: 'createTime', align: 'center', title: '审核时间'}
                        ]],
                    });


                    form.on('submit(examineProductionPlanFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.id = data.id;
                        admin.req('production/productionPlan/admin/productionPlan/auditPlan', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('productionPlanTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'PUT');
                        return false;
                    });
                }
            });
        }

        // 作废
        function toVoidToolCabinet(_data) {
            layer.confirm('确定作废吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('production/productionPlan/admin/productionPlan/cancellation', {id: _data.id}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('productionPlanTable');
                        layer.closeAll('page');
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 创建生产计划
        function showAddProductionPlan(_data, _subData) {
            let addProductionPlanPop = admin.open({
                type: 1,
                area: ['1400px', '500px'],
                title: '添加询生产计划',
                content: $('#addProductionPlanForm').html(),
                success: function () {
                    // 计划来源下拉操作
                    form.on('select(planNature)', function (data) {
                        if (data.value == 'process') {
                            $('#processIdBox').show({duration: 300});
                            $('select[name="processId"]').attr('lay-verify', 'required')
                        } else {
                            $('#processIdBox').hide({duration: 300});
                            $('select[name="processId"]').val('');
                            $('select[name="processId"]').removeAttr('lay-verify')
                        }
                        form.render('select');
                    });

                    admin.reqSync('enterprise/product/admin/processInfo/list', {
                        productTypeId: _subData.proTypeId,
                        page: 1,
                        limit: 99999
                    }, function (res) {
                        if (res.code == 200) {
                            loadSelectHtml(res.data, '请选择工序', 'processId', 'addProductionPlanForm', 1)
                        }
                    }, 'GET');

                    let createProductionPlanTb = table.render({
                        elem: '#addProductionPlanTable',
                        defaultToolbar: false,
                        data: [_subData],
                        page: false,
                        limit: Number.MAX_VALUE,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'proTypeName', align:'center', title: '物料类型'}
                            , {field: 'customerSkuItem', align:'center', title: '客户物料编号'}
                            , {field: 'customerSkuName', align:'center', title: '客户物料名称'}
                            , {field: 'customerSpecCode', align:'center', title: '客户规格码'}
                            , {
                                field: 'orderProEnclosureList', align:'center', title: '附件', templet: d => {
                                    let orderProEnclosureListHtml = '';
                                    d.orderProEnclosureList.forEach((item, index) => {
                                        orderProEnclosureListHtml += '<a href="' + item.fileUrl + '" download="12" target="_blank" class="layui-btn layui-btn-xs layui-btn-warm">查看附件</a></br />'
                                    });
                                    return orderProEnclosureListHtml;
                                }
                            }
                            , {field: 'proUnitName', align:'center', title: '单位'}
                            , {field: 'quantity', align:'center', title: '总需求数量'}
                            , {field: 'residueQuantity', align:'center', title: '剩余需求数量'}
                            , {
                                field: 'productionQuantity',
                                align:'center',
                                title: '计划生产数量',
                                width: 180,
                                templet: d => '<input class="layui-input" lay-verify="number" name="productionQuantity" onkeyup="value=value.replace(/^(0+)|[^\\d]+/g,\'\')"/>'
                            }
                            , {
                                field: 'outputTime',
                                align:'center',
                                title: '计划产出时间',
                                width: 180,
                                templet: d => '<input class="layui-input" lay-verify="required" name="outputTime" id="outputTime"/>'
                            }
                        ]],
                        done: function (res, curr, count) {
                            laydate.render({
                                elem: '#outputTime',
                                type: 'datetime'
                            });
                        }
                    });
                    form.val('addProductionPlanForm', _data);
                    form.render('select');

                    form.on('submit(addProductionPlanFormSubmit)', function (d) {

                        d.field.productTypeId = _subData.proTypeId;
                        d.field.productTypeName = _subData.proTypeName;
                        d.field.customerSkuCode = _subData.customerSkuItem;
                        d.field.customerSkuSpec = _subData.customerSpecCode;
                        d.field.unitId = _subData.proUnitId;
                        d.field.unitName = _subData.proUnitName;
                        d.field.orderType = _data.orderType;
                        d.field.shipmentTime = _data.deliveryTime;
                        d.field.status = 'affirm';
                        d.field.orderId = _data.id;
                        d.field.customerSkuName = _subData.customerSkuName;
                        d.field.planSource = 'order';
                        d.field.demandQuantity = _subData.quantity;
                        d.field.orderProId = _subData.id;

                        if (parseInt(d.field.productionQuantity) > parseInt(_subData.residueQuantity)) {
                            layer.confirm('计划生产数量大于订单剩余计划数量是否继续', {
                                icon: 3,
                                title: '提示',
                                btn: ['提交', '取消']
                            }, function (index) {
                                admin.req('production/productionPlan/admin/productionPlan/add', d.field, function (res) {
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('orderSchedulingTable');
                                        layer.close(addProductionPlanPop);
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }, 'POST');
                                layer.close(index);
                            }, function (index) {
                                layer.msg('已取消');
                                layer.closeAll('loading');
                            });
                        } else {
                            admin.req('production/productionPlan/admin/productionPlan/add', d.field, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('orderSchedulingTable');
                                    layer.close(addProductionPlanPop);
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'POST');
                        }
                        return false;
                    });
                }
            });
        }

        function showOrderDetailsForm(_data) {
            admin.open({
                type: 1,
                area: ['1500px', '700px'],
                title: '详情',
                content: $('#orderDetailsForm').html(),
                success: function () {
                    form.val('orderDetailsForm', _data);
                    let createProductionPlanTb = table.render({
                        elem: '#orderDetailsTable',
                        data: _data.orderProList,
                        page: false,
                        limit: Number.MAX_VALUE,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            , {field: 'proTypeName', align: 'center', title: '物料类型'}
                            , {field: 'customerSkuItem', align: 'center', title: '客户SKU料号'}
                            , {field: 'customerSkuName', align: 'center', title: '客户SKU名称'}
                            , {field: 'customerSpecCode', align: 'center', title: '客户规格码'}
                            , {
                                field: 'orderProEnclosureList', align: 'center', title: '附件', templet: d => {
                                    let orderProEnclosureListHtml = '';
                                    d.orderProEnclosureList.forEach((item, index) => {
                                        orderProEnclosureListHtml += '<a href="' + item.fileUrl + '" download="12" target="_blank" class="layui-btn layui-btn-xs layui-btn-warm">查看附件</a></br />'
                                    });
                                    return orderProEnclosureListHtml;
                                }
                            }
                            , {field: 'proUnitName', align: 'center', title: '单位'}
                            , {field: 'quantity', align: 'center', title: '需求数量'}
                            , {field: 'proRemark', align: 'center', title: '说明'}
                            , {field: 'offerStatusDesc', align: 'center', title: '状态'}
                            , {field: 'offerMoney', align: 'center', title: '最终报价'}
                        ]]
                    });
                }
            })
        }

        // 渲染下拉框数据
        function loadSelectHtml(data, tips, htmltarget, formName, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].id}'>${data[i].nodeNames}</option>`;
                }
            }
            $("form[lay-filter='" + formName + "'] select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>